package bookmanage;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JComboBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class BookRental extends JInternalFrame {

	Connection con;
	private JComboBox comboBox;
	private JTextField searchTF;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel lblNewLabel;
	private JTextField bookname;
	private JButton button;
	private JScrollPane scrollPane_1;
	private JTable table_1;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel label;
	private JLabel label_1;
	private JTextField userid;
	private JTextField nsername;
	private JTextField bookno;
	private JTextField booktitle;
	private JButton btnNewButton_1;
	private JLabel lblNewLabel_3;
	private JTextField schedule_date;
	
	DefaultTableModel model1 = new DefaultTableModel();
	DefaultTableModel model2 = new DefaultTableModel();
	Vector<String> colName1 = new Vector<String>();
	Vector<String> colName2 = new Vector<String>();
	
	
	public BookRental(Connection con) {
		super("대여",true,true,false,true);
		this.con=con;
		
		colName1.add("아이디");
		colName1.add("이름");
		colName1.add("생일");
		colName1.add("폰 번호");
		colName1.add("대여권수");
		
		colName2.add("도서번호");
		colName2.add("제목");
		colName2.add("저자");
		colName2.add("출판사");
		colName2.add("대출 상태");
		
		setBounds(100, 100, 570, 330);
		getContentPane().setLayout(null);
		getContentPane().add(getComboBox());
		getContentPane().add(getSearchTF());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getBookname());
		getContentPane().add(getButton());
		getContentPane().add(getScrollPane_1());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getUserid());
		getContentPane().add(getNsername());
		getContentPane().add(getBookno());
		getContentPane().add(getBooktitle());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblNewLabel_3());
		getContentPane().add(getSchedule_date());
		
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.addItem("아이디");
			comboBox.addItem("이름");
			comboBox.setBounds(12, 10, 78, 21);
		}
		return comboBox;
	}
	private JTextField getSearchTF() {
		if (searchTF == null) {
			searchTF = new JTextField();
			searchTF.setBounds(102, 10, 116, 24);
			searchTF.setColumns(10);
		}
		return searchTF;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
				}
			});
			btnNewButton.setBounds(230, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 46, 315, 83);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			model1 = new DefaultTableModel(colName1, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table = new JTable(model1);
		}
		return table;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("책 제목");
			lblNewLabel.setBounds(12, 139, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getBookname() {
		if (bookname == null) {
			bookname = new JTextField();
			bookname.setColumns(10);
			bookname.setBounds(102, 136, 116, 24);
		}
		return bookname;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("검색");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					model2.setRowCount(0);
					String b_name = bookname.getText().trim();
					if(b_name.equals("")){
						JOptionPane.showMessageDialog(null, "책 제목을 입력해주세요!");
						return;
					}
					String sql="";
					PreparedStatement psmt;
					ResultSet rs;
					try {
						sql="select nvl(count(*),0) from RENT_RETURN where book_no=?";
						psmt = con.prepareStatement(sql);
						psmt.setString(1, b_name);
						/*
						insert into RENT_RETURN(rr_no,m_id,book_no,rent_date,schedule_date)
						values(seq_rr_no.nextval,'choi',1,sysdate,sysdate+7)
						select count(*) from RENT_RETURN where book_no=1
						select nvl(return_date,'2000/01/01') from RENT_RETURN
						where book_no=1 and rent_date is not null order by rent_date desc
						 */
						
						sql="select book_no,b_title,b_author,b_publisher,"
								+ "(select nvl(return_date,0) from RENT_RETURN where book_no=? and rent_date is not null ordery by rent_date desc),"
								+ " from bookinfo where b_title like '%'||?||'%'";
						psmt = con.prepareStatement(sql);
						psmt.setString(1, b_name);
						rs = psmt.executeQuery();
						while(rs.next()){
							Vector<String> vec = new Vector<String>();
							vec.add(rs.getInt(1)+"");
							vec.add(rs.getString(2));
							vec.add(rs.getString(3));
							vec.add(rs.getString(4));
							model2.addRow(vec);
						}
						if(model2.getRowCount()==0){
							JOptionPane.showMessageDialog(null, "검색된 도서가 없습니다.");
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			button.setBounds(230, 139, 97, 23);
		}
		return button;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(12, 178, 315, 113);
			scrollPane_1.setViewportView(getTable_1());
		}
		return scrollPane_1;
	}
	private JTable getTable_1() {
		if (table_1 == null) {
			model2 = new DefaultTableModel(colName2, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table_1 = new JTable(model2);
		}
		return table_1;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("아이디");
			lblNewLabel_1.setBounds(339, 45, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("도서 번호");
			lblNewLabel_2.setBounds(339, 115, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("이름");
			label.setBounds(339, 80, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("책 제목");
			label_1.setBounds(339, 150, 57, 15);
		}
		return label_1;
	}
	private JTextField getUserid() {
		if (userid == null) {
			userid = new JTextField();
			userid.setEditable(false);
			userid.setBounds(408, 45, 116, 24);
			userid.setColumns(10);
		}
		return userid;
	}
	private JTextField getNsername() {
		if (nsername == null) {
			nsername = new JTextField();
			nsername.setEditable(false);
			nsername.setColumns(10);
			nsername.setBounds(408, 80, 116, 24);
		}
		return nsername;
	}
	private JTextField getBookno() {
		if (bookno == null) {
			bookno = new JTextField();
			bookno.setEditable(false);
			bookno.setColumns(10);
			bookno.setBounds(408, 115, 116, 24);
		}
		return bookno;
	}
	private JTextField getBooktitle() {
		if (booktitle == null) {
			booktitle = new JTextField();
			booktitle.setEditable(false);
			booktitle.setColumns(10);
			booktitle.setBounds(408, 150, 116, 24);
		}
		return booktitle;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("대여");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String m_id = userid.getText().trim();
					String b_no = bookno.getText().trim();
					String s_date = schedule_date.getText().trim();
					Date d = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
					String r_date = sdf.format(d);
					s_date = s_date.substring(2, s_date.length());
					if(m_id.equals("")){
						JOptionPane.showMessageDialog(null, "대여자를 선택해주세요!");
						return;
					}
					if(b_no.equals("")){
						JOptionPane.showMessageDialog(null, "대여 도서를 선택해주세요!");
						return;
					}
					
					try {
						String sql = "insert into RENT_RETURN(rr_no,m_id,book_no,rent_date,schedule_date) "
								+ "values(seq_rr_no.nextval,?,?,?,?)";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, m_id);
						psmt.setInt(2, Integer.parseInt(b_no));
						psmt.setString(3, r_date);
						psmt.setString(4, s_date);
						int ct = psmt.executeUpdate();
						if(ct==1){
							JOptionPane.showMessageDialog(null, "대여 되었습니다");
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btnNewButton_1.setBounds(429, 224, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("반납 예정일");
			lblNewLabel_3.setBounds(339, 185, 69, 21);
		}
		return lblNewLabel_3;
	}
	private JTextField getSchedule_date() {
		if (schedule_date == null) {
			schedule_date = new JTextField();
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 7);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			String d7 = sdf.format(cal.getTime());
			schedule_date.setText(d7);
			schedule_date.setEditable(false);
			schedule_date.setColumns(10);
			schedule_date.setBounds(408, 185, 116, 24);
		}
		return schedule_date;
	}
}
